package com.itheima.test;

import com.itheima.dao.IUserDao;
import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class testUser {

    private InputStream in;
    private SqlSession sqlSession;
    private IUserDao userDao;
    private  SqlSessionFactory factory;

    @Before //用于在测试方法之前执行
    public  void  init () throws Exception{
        //1.读取配置文件，生成字节输入流
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.获取sqlSessionFactory
        factory = new SqlSessionFactoryBuilder().build(in);
        //3.获取sqlSession对象
        sqlSession = factory.openSession();
        //4.获取dao的代理对象
        userDao = sqlSession.getMapper(IUserDao.class);
    }
    @After //用于在测试方法之后执行
    public  void  destory() throws Exception{
        //提交事务
        sqlSession.commit();
        //6.释放资源
        sqlSession.close();
        in.close();
    }
    /**
     * 测试一级缓存
     */
    @Test
    public void testFirstLevelCache(){
        User user1 = userDao.findById(41);
        System.out.println(user1);
//        sqlSession.close();
//        sqlSession = factory.openSession();
        sqlSession.clearCache();
        userDao = sqlSession.getMapper(IUserDao.class);
        User user2 = userDao.findById(41);
        System.out.println(user2);
        System.out.println(user1==user2);
    }

    /**
     * 测试缓存的同步
     *
     */
    @Test
    public void testClearCache(){
        User user1 = userDao.findById(41);
        System.out.println(user1);

        user1.setUsername("upadte user clear");
        user1.setAddress("江西南昌");
        userDao.updateUser(user1);

        User user2 = userDao.findById(41);
        System.out.println(user2);
        System.out.println(user1==user2);
    }



}
